/*      CG2 - Animation
       Uebung 03
       Swen Priebe, 1061274
modified by Michael Antemann 1061753

                              */


double abFunc(double (*func)(double), double x) {
       double sw = 1e-6;       // schrittweite
       double wert;
       wert = ((func( x + sw) - func( x)) / sw);
       if( wert == 0.0 ){
    	   return  sw;
       }
       else
       return wert ;
}

double newton(double (*func)(double), double winkel_c, double
winkel_v) {
       double xk = winkel_v, xk1 = winkel_v;


       // newton-iteration
  //     printf("xk1 = xk - f(x) / f'(x)\n");
       do {
//    	   printf("xk : %f  , xk+1: %f\n" ,xk ,xk1);

               xk = xk1;
//               printf("%f = %f - %f / %f = ",xk1,xk,func( xk), abFunc(func, xk)) ;
               xk1 = xk - (func( xk) / abFunc(func, xk));
////               printf("%f \n",xk1) ;
               if(fabsl(abFunc(func, xk1)) == 1e-6 && fabsl(func( xk1))>=1e-6){
            	   return xk;
               }
       }
       while((1-fabsl(xk)/fabsl(xk1)) >= 1e-6 && fabsl(xk - xk1) >= 1e-6 && fabsl(func( xk1)) >=1e-6);

       return xk1;
}
